using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

namespace RoboSmartIRobotCreate
{
    /// <summary>
    /// Serial port connection
    /// </summary>
    public class Connection
    {
        private SerialPort serialPort; 
        private int comPort;
        private int baudRate;
        private int dataBits;
        private Parity parity;
        private StopBits stopBits;

        public int Available
        {
            get { return serialPort.BytesToRead; }
            
        }


        public delegate void DataReceivedDelegate (object sender, SerialDataReceivedEventArgs e);
        public event DataReceivedDelegate DataReceivedEventHandler;

        public bool IsConnectionOpen
        {
            get
            {
                return serialPort.IsOpen;
            }

        }

        public StopBits StopBits
        {
            get 
            {
                return stopBits; 
            }
            set 
            {
                stopBits = value;
                serialPort.StopBits = stopBits;
            }
        }

        public Parity Parity
        {
            get 
            {
                return parity; 
            }
            set 
            {
                parity = value;
                serialPort.Parity = parity;
            }
        }
	

        public int DataBits
        {
            get 
            {
                return dataBits; 
            }
            set 
            {
                dataBits = value;
                serialPort.DataBits = dataBits;

            }
        }
	
	

        public int BaudRate
        {
            get 
            {
                return baudRate; 
            }
            set 
            {
                baudRate = value;
                serialPort.BaudRate = baudRate;

            }
        }
	

        public int ComPort
        {
            get 
            {
                return comPort;
            }
            set 
            {
                comPort = value;
                serialPort.PortName = string.Format("COM{0}", ComPort);
            }
        }

        /// <summary>
        /// Default constructor; just create the connection
        /// without specifying port, baud rate and 
        /// </summary>
        public Connection()
        {
            serialPort = new SerialPort();
        }

        /// <summary>
        /// Initializes a connection on the COM port specified by the number
        /// 
        /// Exceptions:
        /// System.IO.IOException
        /// </summary>
        /// <param name="port"></param>
        public Connection(byte port)
        {
            serialPort = new SerialPort(string.Format("COM{0}", port));
            if (serialPort != null)
            {
                serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            }
        }

        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DataReceivedEventHandler(sender, e);
        }

        /// <summary>
        /// Opens a connection on COM port
        /// </summary>
        /// <returns>True if connected successfully, false otherwise</returns>
        public bool Open()
        {
            try
            {
                serialPort.Open();
            }
            catch 
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Close connection
        /// </summary>
        public void Close()
        {
            serialPort.Close();
        }

        /// <summary>
        /// Send byte instructions over the connection
        /// </summary>
        /// <param name="data">co</param>
        /// <returns></returns>
        public void WriteBytes(byte[] data)
        {
            serialPort.Write(data, 0, data.Length);
        }

        public int ReadBytes(byte[] buffer)
        {
            return serialPort.Read(buffer, 0, buffer.Length);
        }

        public int ReadByte()
        {
            return serialPort.ReadByte();
        }
	            
        public string ReadExisting()
        {
            return serialPort.ReadExisting();
        }

    }
}
